//
//  Login_Vc.swift
//  LocaLink
//
//  Created by eme on 2016/12/12.
//  Copyright © 2016年 Icy. All rights reserved.
//

/*
 owner:cy
 update:2016年12月21日16:03:29
 info: 登陆界面
 model: UserModel
 viewmodel: LoginViewModel
 */
import RxSwift
import UtilCore
import SwiftyUserDefaults
import SwiftyJSON

class Login_Vc: Base_Vc {
    
    /****************************Storyboard UI设置区域****************************/
    
    @IBOutlet weak var signIn_Btn: UIButton!
    
    @IBOutlet weak var email_Tf: UITextField!
    
    @IBOutlet weak var password_Tf: UITextField!
    
    @IBOutlet weak var forget_Btn: UIButton!

    @IBOutlet weak var register_Btn: FooterLineButton!
    
    @IBOutlet weak var manager_Btn: UIButton!
    
    /*----------------------------   自定义属性区域    ----------------------------*/
    
    var manageVm:Login_Vm?


    /****************************Storyboard 绑定方法区域****************************/
    
    
    
    /**************************** 以下是方法区域 ****************************/
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }
    /**
     界面基础设置
     */
    override func setupUI() {
        /**
         *  自定义 导航栏左侧 返回按钮
         */
        self.customLeftBarButtonItem()
        
        self.signIn_Btn.changeBorderColor(UIColor.signIn_Separate_Line(), cornerRadius: self.signIn_Btn.frame.size.height/2)
        self.manager_Btn.changeBorderColor(UIColor.signIn_Separate_Line(), cornerRadius: 6)

//        self.email_Tf.changePlaceholder("example@email.com")
//        self.password_Tf.changePlaceholder("password")
        
    }
    
    /**
     绑定到viewmodel 设置
     */
    override func bindToViewModel(){
        
        self.manageVm = Login_Vm()
        
        self.manageVm?.loginSuccess
            .subscribe(onNext: { [unowned self] ( data ) in
                if let loginUser = LoginModel(json: data), loginUser.success {
                    let appdelegate = UIApplication.shared.delegate as! AppDelegate
                    appdelegate.toMapVc()
//                    self.loginedUser.model = loginUser
                }else{
                    self.view.toast(data["message"].stringValue)
                }
            })
            .disposed(by: disposeBag)
        
        
        self.manageVm?.loading.asObservable().bindTo(self.view.rx_loading).disposed(by: disposeBag)
        self.signIn_Btn
            .rx.tap
            .subscribe(onNext: { [unowned self] ( _ ) in
               self.toLogin()
            })
            .disposed(by: disposeBag)

        self.manager_Btn
            .rx.tap
            .subscribe(onNext: { ( _ ) in
                _ = "http://101.200.203.219:920/login.html".openURL()
            })
            .disposed(by: disposeBag)
       
    }
    /// 点击登录
    func toLogin() -> Void {
        guard let email = self.email_Tf.text, email.characters.count > 0 else {
            self.view.toast("email is not nil")
            return
        }
        guard let password = self.password_Tf.text, password.characters.count > 0 else {
            self.view.toast("password is not nil")
            return
        }
        Defaults[.username] = email
        Defaults[.password] = password
        self.manageVm?.loginSubject.onNext((email,password))
        
    }

    
    
}
